<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input v-model="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input v-model="userInfo.workNumber" disabled size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <!-- ! 是转成布尔值并取反 -->
                <!-- !! 是转成布尔值 -->
                <el-input
                  v-model="userInfo.mobile"
                  size="mini"
                  class="inputW"
                  :disabled="!!$route.params.id"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <SelectTree v-model="userInfo.departmentId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                  <el-option label="正式" :value="1" />
                  <el-option label="非正式" :value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userInfo.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <!-- value-format="yyyy-MM-dd" 绑定的日期格式 -->
                <el-date-picker
                  v-model="userInfo.correctionTime"
                  size="mini"
                  type="date"
                  class="inputW"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片 -->
                <ImageUpload v-model="userInfo.staffPhoto" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left:220px">
              <el-button size="mini" type="primary" @click="submit">保存更新</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { addEmployee, getEmployeeDetail, updateEmployee } from '@/api/employee'
import SelectTree from './components/select-tree.vue'
import ImageUpload from './components/image-upload.vue'
export default {
  components: {
    SelectTree,
    ImageUpload
  },
  data() {
    return {
      userInfo: {
        username: '',
        mobile: '',
        departmentId: null,
        formOfEmployment: '',
        timeOfEntry: '',
        correctionTime: '',
        workNumber: '',
        staffPhoto: ''
      },
      rules: {
        username: [
          { required: 'true', message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '请输入 1 ~ 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: 'true', message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入手机号', trigger: 'blur' }
        ],
        departmentId: [
          { required: 'true', message: '请选择部门', trigger: 'change' }
        ],
        formOfEmployment: [
          { required: 'true', message: '请选择聘用形式', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: 'true', message: '请选择入职时间', trigger: 'change' }
        ],
        correctionTime: [
          { required: 'true', message: '请选择转正时间', trigger: 'change' },
          {
            validator: (rule, val, cb) => {
              // 转正时间不能小于入职时间
              // console.log('转正时间:', +new Date(val))
              // console.log('入职时间:', +new Date(this.userInfo.timeOfEntry))
              if (+new Date(this.userInfo.timeOfEntry) > +new Date(val)) {
                cb(new Error('转正时间不能小于入职时间'))
              } else {
                cb()
              }
            },
            trigger: 'change'
          }
        ],
        workNumber: []
      }
    }
  },
  async created() {
    // 该页面可能接收到参数, 也可能没有参数
    const id = this.$route.params.id
    // console.log(id)
    // 判断是否有 id, 有 id 才是查看员工, 才需要数据回显
    if (id) {
      this.userInfo = await getEmployeeDetail(id)
    }
  },
  methods: {
    async submit() {
      // 兜底校验
      await this.$refs.userForm.validate()
      // 有 id 说明现在是编辑
      if (this.userInfo.id) {
        await updateEmployee(this.userInfo)
        // 提示用户
        this.$message.success('修改员工成功')
      } else {
        // 调接口发请求
        await addEmployee(this.userInfo)
        // 提示用户
        this.$message.success('新增员工成功')
      }
      // 跳回列表页
      this.$router.push('/employee')
    }
  }
}
</script>

<style scoped lang="scss">
    .edit-form {
      background: #fff;
      padding: 20px;
      .inputW {
        width: 380px
      }
    }

</style>
